package cn.iocoder.yudao.module.book.controller.admin.collect;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.collect.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.collect.CollectDO;
import cn.iocoder.yudao.module.book.service.collect.CollectService;

@Tag(name = "管理后台 - 收藏")
@RestController
@RequestMapping("/book/collect")
@Validated
public class CollectController {

    @Resource
    private CollectService collectService;

    @PostMapping("/create")
    @Operation(summary = "创建收藏")
    @PreAuthorize("@ss.hasPermission('book:collect:create')")
    public CommonResult<Integer> createCollect(@Valid @RequestBody CollectSaveReqVO createReqVO) {
        return success(collectService.createCollect(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收藏")
    @PreAuthorize("@ss.hasPermission('book:collect:update')")
    public CommonResult<Boolean> updateCollect(@Valid @RequestBody CollectSaveReqVO updateReqVO) {
        collectService.updateCollect(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收藏")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:collect:delete')")
    public CommonResult<Boolean> deleteCollect(@RequestParam("id") Integer id) {
        collectService.deleteCollect(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收藏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:collect:query')")
    public CommonResult<CollectRespVO> getCollect(@RequestParam("id") Integer id) {
        CollectDO collect = collectService.getCollect(id);
        return success(BeanUtils.toBean(collect, CollectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收藏分页")
    @PreAuthorize("@ss.hasPermission('book:collect:query')")
    public CommonResult<PageResult<CollectRespVO>> getCollectPage(@Valid CollectPageReqVO pageReqVO) {
        PageResult<CollectDO> pageResult = collectService.getCollectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CollectRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收藏 Excel")
    @PreAuthorize("@ss.hasPermission('book:collect:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCollectExcel(@Valid CollectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CollectDO> list = collectService.getCollectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收藏.xls", "数据", CollectRespVO.class,
                        BeanUtils.toBean(list, CollectRespVO.class));
    }

}